package com.nova.bluetooth.ui.device;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.dido.fastblelib.utils.HexUtil;
import com.nova.bluetooth.MyApp;
import com.nova.bluetooth.R;
import com.nova.bluetooth.base.BaseFragment;
import com.nova.bluetooth.databinding.FragmentEqSetBinding;
import com.nova.bluetooth.helpUtils.BluetoothDataUtil;
import com.nova.bluetooth.manage.BluetoothHelper;
import com.nova.bluetooth.myEnum.EQModel;
import com.nova.bluetooth.ui.bluetoothDemo.NovaBluetoothControlViewModel;

/**
 * eq 设置
 */
public class EQSettingFragment extends BaseFragment {
    private FragmentEqSetBinding binding;
    NovaBluetoothControlViewModel deviceViewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_eq_set, container, false);
        binding = FragmentEqSetBinding.bind(view);
        deviceViewModel = new ViewModelProvider(MyApp.getInstance()).get(NovaBluetoothControlViewModel.class);
        binding.setOnClick(onClickEvent);
        initData();
        initView();
        observe();
        return binding.getRoot();
    }

    private void initData() {
        byte[] geEqCMD = BluetoothDataUtil.calculateCheckSumResult(
                HexUtil.hexStringToBytes(BluetoothDataUtil.BLE_CMD_GET_EQ));
        BluetoothHelper.getInstance().sendByteArrayDataSafety(geEqCMD);
    }

    private void initView() {
        binding.ivBack.setOnClickListener(onClickEvent);
        binding.layoutEq1.setSelected(true);
    }

    private void observe() {
        deviceViewModel.eqModelMutableLiveData.observe(getViewLifecycleOwner(),
                eqModelMutableLiveData -> {
            updateEqUI(eqModelMutableLiveData);
        });
    }


    private void selectEq(View view) {
        EQModel eqModel = EQModel.SSQVP;
        if (view.getId() == R.id.layout_eq_1) {
            eqModel = EQModel.SSQVP;
        } else if (view.getId() == R.id.layout_eq_2) {
            eqModel = EQModel.POP;
        } else if (view.getId() == R.id.layout_eq_3) {
            eqModel = EQModel.ROCK;
        } else if (view.getId() == R.id.layout_eq_4) {
            eqModel = EQModel.CLASSIC;
        } else if (view.getId() == R.id.layout_eq_5) {
            eqModel = EQModel.LYRIC;
        } else if (view.getId() == R.id.layout_eq_6) {
            eqModel = EQModel.SOFT;
        }

        updateEqUI(eqModel);
        BluetoothHelper.getInstance().sendByteArrayDataSafety(
                BluetoothDataUtil.getEQSetCMD(eqModel));
    }


    private void updateEqUI(EQModel eqModel) {
        binding.layoutEq1.setSelected(false);
        binding.layoutEq2.setSelected(false);
        binding.layoutEq3.setSelected(false);
        binding.layoutEq4.setSelected(false);
        binding.layoutEq5.setSelected(false);
        binding.layoutEq6.setSelected(false);
        switch (eqModel) {
            case SSQVP:
                binding.layoutEq1.setSelected(true);
                break;
            case POP:
                binding.layoutEq2.setSelected(true);
                break;
            case ROCK:
                binding.layoutEq3.setSelected(true);
                break;
            case CLASSIC:
                binding.layoutEq4.setSelected(true);
                break;
            case LYRIC:
                binding.layoutEq5.setSelected(true);
                break;
            case SOFT:
                binding.layoutEq6.setSelected(true);
                break;
        }
    }

    private View.OnClickListener onClickEvent = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.iv_back) {
                getActivity().finish();
            } else {
                selectEq(v);
            }
        }
    };
}
